package cmd

import (
	"APT/internal/crons"
	"APT/internal/dao"
	"APT/internal/library/hgorm/hook"
	"APT/internal/model/entity"
	"APT/internal/service"
	"context"
	"database/sql"
	"errors"
	"fmt"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gcmd"
	"github.com/gogf/gf/v2/os/gtime"
	"strings"
)

var (
	CronFeeds = &gcmd.Command{
		Name:  "CronListenFeeds",
		Usage: "start",
		Brief: "自动接收私域订单变更信息",
		Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
			return crons.AhPublicApiFeeds(ctx)
		},
	}

	CronAvailabilities = &gcmd.Command{
		Name:  "CronSyncAvailabilities",
		Usage: "start",
		Brief: "自动接收可售房变更信息",
		Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
			var (
				PmsProperty []entity.PmsProperty
				PmsRoomType []entity.PmsRoomType
				puidNum     int64
				puidName    string
				tuidNum     int64
				tuidName    string
				runner      string
			)
			puidString := parser.GetOpt("puid").String()
			tuidString := parser.GetOpt("tuid").String()
			IsOptions := parser.GetOpt("op").String()
			if IsOptions == "Y" {
				if g.IsEmpty(puidString) && g.IsEmpty(tuidString) {
					// 查询物业
					if err = dao.PmsProperty.Ctx(ctx).Hook(hook.PmsFindLanguageValueHook).Scan(&PmsProperty); err != nil && !errors.Is(err, sql.ErrNoRows) {
						g.Log().Error(ctx, err)
					} else {
						for i := range PmsProperty {
							fmt.Printf("序号 【%d】 --  物业ID【%s】  -- 物业名 【%s】\n", i+1, PmsProperty[i].Uid, PmsProperty[i].Name)
						}
						fmt.Printf("请选择物业序号\n")
						fmt.Scan(&puidNum)
						if puidNum != 0 {
							puidString = PmsProperty[puidNum-1].Uid
							puidName = PmsProperty[puidNum-1].Name
							fmt.Printf("选择的物业Uid是 %s\n", puidString)
							fmt.Printf("选择的物业是 %s\n", puidName)
						}
					}
					// 查询房型
					if err = dao.PmsRoomType.Ctx(ctx).Where("puid", puidString).OmitEmptyWhere().Hook(hook.PmsFindLanguageValueHook).Scan(&PmsRoomType); err != nil && !errors.Is(err, sql.ErrNoRows) {
						g.Log().Error(ctx, err)
					} else {
						for i := range PmsRoomType {
							fmt.Printf("序号 【%d】 --  房型ID【%s】  -- 房型名 【%s】-- 价格PLAN_ID 【%s】\n", i+1, PmsRoomType[i].Uid, PmsRoomType[i].Name, PmsRoomType[i].RatePlanId)
						}
						fmt.Scan(&tuidNum)
						if tuidNum != 0 {
							tuidString = PmsRoomType[tuidNum-1].Uid
							tuidName = PmsRoomType[tuidNum-1].Name
						}
					}
				}
				fmt.Printf("puid:%s  name:%s\n", puidString, puidName)
				fmt.Printf("tuid:%s  name:%s\n", tuidString, tuidName)
				fmt.Println("是否执行同步库存操作   Y/N default Y")
				fmt.Scan(&runner)
			}
			if strings.ToUpper(runner) == "N" {
				fmt.Println("执行终止")
				return
			}
			return crons.AhPublicApiAvailabilities(ctx, puidString, tuidString)
		},
	}

	CronRoomTypeAndRates = &gcmd.Command{
		Name:  "CronSyncRoomTypeAndRates",
		Usage: "start",
		Brief: "自动接收房型和房态信息",
		Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
			return crons.AhPublicApiRoomTypeAndRates(ctx)
		},
	}

	CronProperties = &gcmd.Command{
		Name:  "CronSyncProperties",
		Usage: "start",
		Brief: "同步房源信息",
		Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
			uidString := parser.GetOpt("uid").String()
			return crons.AhPublicApiProperties(ctx, uidString)
		},
	}
	CronFoodSettlement = &gcmd.Command{
		Name:  "CronFoodSettlement",
		Usage: "start",
		Brief: "餐饮订单结算",
		Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
			g.DB().SetLogger(g.Log().Path("logs/CRON/SETTLEMENT"))
			if err = service.FoodSettlementOrder().DailySettlement(ctx); err != nil {
				return
			}
			if err = service.FoodSettlementOrder().WeekSettlement(ctx); err != nil {
				return
			}
			if err = service.FoodSettlementOrder().MonthSettlement(ctx); err != nil {
				return
			}
			return
		},
	}
	CronCarSettlement = &gcmd.Command{
		Name:  "CronCarSettlement",
		Usage: "start",
		Brief: "接送机订单结算",
		Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
			g.DB().SetLogger(g.Log().Path("logs/CRON/SETTLEMENT"))
			if err = service.CarSettlementOrder().DailySettlement(ctx); err != nil {
				return
			}
			if err = service.CarSettlementOrder().WeekSettlement(ctx); err != nil {
				return
			}
			if err = service.CarSettlementOrder().MonthSettlement(ctx); err != nil {
				return
			}
			return
		},
	}
	CronSpaSettlement = &gcmd.Command{
		Name:  "CronSpaSettlement",
		Usage: "start",
		Brief: "按摩订单结算",
		Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
			g.DB().SetLogger(g.Log().Path("logs/CRON/SETTLEMENT"))
			if err = service.SpaSettlementOrder().DailySettlement(ctx); err != nil {
				return
			}
			if err = service.SpaSettlementOrder().WeekSettlement(ctx); err != nil {
				return
			}
			if err = service.SpaSettlementOrder().MonthSettlement(ctx); err != nil {
				return
			}
			return
		},
	}
	CronCouponExp = &gcmd.Command{
		Name:  "CronCouponExp",
		Usage: "start",
		Brief: "优惠券过期",
		Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
			g.DB().SetLogger(g.Log().Path("logs/CRON/CronCouponExp"))
			var (
				PmsCoupons []*entity.PmsCoupon
			)
			if err = dao.PmsCoupon.Ctx(ctx).
				Where(dao.PmsCoupon.Columns().State, 1).Scan(&PmsCoupons); err != nil && !errors.Is(err, sql.ErrNoRows) {
				return
			}

			if len(PmsCoupons) < 0 {
				err = gerror.New("没有优惠券过期")
				g.Log().Info(ctx, "没有优惠券过期")
				return
			}

			for _, coupon := range PmsCoupons {
				if !g.IsEmpty(coupon.EndTime) && gtime.Now().After(coupon.EndTime) {
					if _, err = dao.PmsCoupon.Ctx(ctx).WherePri(coupon.Id).Update(g.Map{
						dao.PmsCoupon.Columns().State: 3,
					}); err != nil {
						return
					}
					g.Log().Infof(ctx, "优惠券ID：%d 优惠券过期", coupon.Id)
				}
			}
			g.Log().Info(ctx, "优惠券过期处理完成")
			return
		},
	}
)
